package com.ruoyi.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.bwie.until.R;
import com.ruoyi.pojo.Order;
import com.ruoyi.service.OrderService;
import com.ruoyi.until.QRCodeUtil;
import com.ruoyi.until.WxPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: liufei
 * @Desc:
 * @create: 2024-11-07 12:36
 **/
@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    WxPayUtils wxPayUtils;

    @Autowired
    OrderService orderService;

    @GetMapping("/wxPay/{orderId}")
    public void wxPay(@PathVariable String orderId, HttpServletResponse response){

        String s = wxPayUtils.wxPay(orderId);

        QRCodeUtil.createQRCode(response,s);

    }

    @GetMapping("/resutle/{orderId}")
    public R resutle(@PathVariable String orderId, HttpServletResponse response) throws Exception {
        Boolean b = wxPayUtils.refreshWxPay(orderId);
        if(b){
            System.out.println("支付成功");
            LambdaUpdateWrapper<Order> wrapper1 = new LambdaUpdateWrapper<>();
            wrapper1.eq(Order::getOrderId, orderId).set(Order::getOrderStatus, "支付完成");
            orderService.update(wrapper1);

            return R.ok();
        }else{
            System.out.println("支付失败");
            return R.error("支付失败");
        }
        }
    }


